package com.cw.base.jeeyc.biz.converter;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.base.jeeyc.dao.model.SysExceptionTradeDo;
import com.cw.base.jeeyc.sys.dto.SysExceptionTradeDto;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.wrapper.BaseWrapper;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

import cn.hutool.core.util.StrUtil;

/**
 * 异常登记转换类
 *
 * @author admin
 * @date 2020-08-02 12:07:20
 */
public class SysExceptionTradeConverter {

    /**
     * do转dto
     *
     * @param model
     * @return
     */
    public static SysExceptionTradeDto do2Dto(SysExceptionTradeDo model) {
        if (model == null) {
            return null;
        }
        return SysExceptionTradeDto.builder()
            .id(model.getId())
            .tradeType(model.getTradeType())
            .serialNo(model.getSerialNo())
            .params(model.getParams())
            .applicationEnName(model.getApplicationEnName())
            .applicationCnName(model.getApplicationCnName())
            .interfacePath(model.getInterfacePath())
            .interfaceName(model.getInterfaceName())
            .interfaceMethod(model.getInterfaceMethod())
            .httpMethod(model.getHttpMethod())
            .interfaceParamNames(model.getInterfaceParamNames())
            .interfaceVersion(model.getInterfaceVersion())
            .callbackTime(model.getCallbackTime())
            .ask(model.getAsk())
            .answer(model.getAnswer())
            .createBy(model.getCreateBy())
            .createDate(model.getCreateDate())
            .updateBy(model.getUpdateBy())
            .updateDate(model.getUpdateDate())
            .build();
    }

    /**
     * dto转do
     *
     * @param dto
     * @return
     */
    public static SysExceptionTradeDo dto2Do(SysExceptionTradeDto dto) {
        if (dto == null) {
            return null;
        }
        return SysExceptionTradeDo.builder()
            .id(dto.getId())
            .tradeType(dto.getTradeType())
            .serialNo(dto.getSerialNo())
            .params(dto.getParams())
            .applicationEnName(dto.getApplicationEnName())
            .applicationCnName(dto.getApplicationCnName())
            .interfacePath(dto.getInterfacePath())
            .interfaceName(dto.getInterfaceName())
            .interfaceMethod(dto.getInterfaceMethod())
            .httpMethod(dto.getHttpMethod())
            .interfaceParamNames(dto.getInterfaceParamNames())
            .interfaceVersion(dto.getInterfaceVersion())
            .callbackTime(dto.getCallbackTime())
            .ask(dto.getAsk())
            .answer(dto.getAnswer())
            .createBy(dto.getCreateBy())
            .createDate(dto.getCreateDate())
            .updateBy(dto.getUpdateBy())
            .updateDate(dto.getUpdateDate())
            .build();
    }

    /**
     * doList转dtoList
     *
     * @param doList
     * @return
     */
    public static List<SysExceptionTradeDto> doList2DtoList(List<SysExceptionTradeDo> doList) {
        if (CollectionUtil.isEmpty(doList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(doList.iterator(), SysExceptionTradeConverter::do2Dto));
    }

    /**
     * dtoList转doList
     *
     * @param dtoList
     * @return
     */
    public static List<SysExceptionTradeDo> dtoList2DoList(List<SysExceptionTradeDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(dtoList.iterator(), SysExceptionTradeConverter::dto2Do));
    }

    public static LambdaQueryWrapper buildWrapper(SysExceptionTradeDto request) {
        LambdaQueryWrapper<SysExceptionTradeDo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        BaseWrapper<SysExceptionTradeDo> baseWrapper = new BaseWrapper<>();
        baseWrapper.buildBaseWrapper(lambdaQueryWrapper, request);
        if (null != request.getId()) {
            lambdaQueryWrapper.eq(SysExceptionTradeDo::getId, request.getId());
        }
        if (null != request.getSerialNo()) {
            lambdaQueryWrapper.eq(SysExceptionTradeDo::getSerialNo, request.getSerialNo());
        }
        if (StrUtil.isNotBlank(request.getApplicationEnName())) {
            lambdaQueryWrapper.eq(SysExceptionTradeDo::getApplicationEnName, request.getApplicationEnName());
        }
        if (StrUtil.isNotBlank(request.getApplicationCnName())) {
            lambdaQueryWrapper.eq(SysExceptionTradeDo::getApplicationCnName, request.getApplicationCnName());
        }
        return lambdaQueryWrapper;
    }
}